/**
* Change logs
* 
*/
var ab_po_cf_save_id="";
function ab_po_cf_form_init()
{
    jQuery("#"+ab_po_cf_save_id+"_ring").val(-1);
    jQuery("#"+ab_po_cf_save_id+"_fhour").val(0);
    jQuery("#"+ab_po_cf_save_id+"_thour").val(0);
    jQuery("#"+ab_po_cf_save_id+"_thour").val("0");
    jQuery("#"+ab_po_cf_save_id+"_tmin").val("15");
    jQuery("#"+ab_po_cf_save_id+"_interval").val(0);
    ab_po_cf_rebuildInterval();
    jQuery(".reg_panel").hide();
}

// Active remove button for jQuery
function ab_po_cf_active_removebutton(){
    jQuery(".po_cd_button").click(function(){
        var pa=jQuery(this).parent();
        var id=pa.attr("id");
        jQuery(this).addClass("po_cd_button_ajax");
        jQuery.post('index.php',{
            module: 'prayonline_createuserformmodule',
            action: 'register_remove',
            ajax_action: 1,
            id: id
        }, function(data) {
            data=ajax_string_dim(data);
            if (data=="OK") 
            {
                pa.fadeToggle("fast");
                jQuery(pa).remove();
            }
            else
            {
                jQuery(this).removeClass("po_cd_button_ajax");
            }
        });
    });
}

function ab_po_cf_calculate_finishtime()
{
    var fh=jQuery("#"+ab_po_cf_save_id+"_fhour").val();
    var fm=jQuery("#"+ab_po_cf_save_id+"_fmin").val();
    var inter=jQuery("#"+ab_po_cf_save_id+"_interval").val();

    fh=parseInt(fh);fm=parseInt(fm);interval=parseInt(inter);

    var th=fh; var tm=fm;
    var remain_inter=96-(fh*4+fm/15);
    if (interval > remain_inter)
        {
        interval=remain_inter;
    }
    var new_time=fh*4+fm/15+interval;
    th=Math.floor(new_time/4);
    tm=(new_time % 4)*15;

    jQuery("#"+ab_po_cf_save_id+"_fhour").val(fh);
    jQuery("#"+ab_po_cf_save_id+"_fmin").val(fm);
    jQuery("#"+ab_po_cf_save_id+"_thour").val(th);
    jQuery("#"+ab_po_cf_save_id+"_tmin").val(tm); 

    ab_po_cf_getCurrentRegister(fh, fm, th, tm);
}

// this function rebuild interval combobox
// event raised when 
var ab_po_cf_getCurrentRegisterWorking=false;
function ab_po_cf_rebuildInterval()
{
    if (ab_po_cf_getCurrentRegisterWorking) return;
    ab_po_cf_getCurrentRegisterWorking=true;
    // calculate remain interval
    var fh=jQuery("#"+ab_po_cf_save_id+"_fhour").val();
    var fm=jQuery("#"+ab_po_cf_save_id+"_fmin").val();
    var inter=jQuery("#"+ab_po_cf_save_id+"_interval").val();

    fh=parseInt(fh);fm=parseInt(fm);interval=parseInt(inter);

    var th=fh; var tm=fm;
    var remain_inter=96-(fh*4+fm/15);
    // first, clear all current item
    $('#'+ab_po_cf_save_id+"_interval").children().remove();
    for (var i=1;i<=remain_inter;i++)
    {
        $('#'+ab_po_cf_save_id+"_interval").append("<option value="+i+">"+i+"</option>");
    }
    
    // Apr 16, 2011 - trungdt - Now , we set value for interval
    if (interval > remain_inter)
    {
        interval = remain_inter;
    }
    $('#'+ab_po_cf_save_id+"_interval").val(interval);
    ab_po_cf_getCurrentRegisterWorking=false;
}

// get how many user register selected time
function ab_po_cf_getCurrentRegister(fh, fm, th, tm)
{
    if (ab_po_cf_getCurrentRegisterWorking) return;
    ab_po_cf_getCurrentRegisterWorking=true;
    ajax_loader_show(jQuery("#lbfs"+ab_po_cf_save_id));
    jQuery.post('index.php',{
        module: 'prayonline_createuserformmodule',
        action: 'get_localtime',
        ajax_action: 1,
        fh: fh, 
        fm: fm,
        th: th, 
        tm: tm, 
    }, function(data) {
        data=ajax_string_dim(data);
        if (data!="") // load failed
        {
            var obj = eval('(' + data + ')');
            jQuery(".reg_info_value").html(obj.sum);
            jQuery(".local_time_value").html(obj.local_time);
            jQuery("#"+ab_po_cf_save_id+"_info").fadeIn();
        }
        ajax_loader_restore(jQuery("#lbfs"+ab_po_cf_save_id));
        ab_po_cf_getCurrentRegisterWorking=false;
    });
}

function ab_po_cf_createsubmit(name)
{
    ab_po_cf_save_id=name;
    jQuery(document).ready(function(){
        //
        ab_po_cf_active_removebutton();

        // form init
        jQuery("#b"+ab_po_cf_save_id).click(function(){
            ab_po_cf_form_init();
            jQuery("#f"+ab_po_cf_save_id).fadeToggle("normal");
        });

        // form submit
        jQuery("#bfs"+ab_po_cf_save_id).click(function(){
            var ring="";
            var fh=jQuery("#"+ab_po_cf_save_id+"_fhour").val();
            var fm=jQuery("#"+ab_po_cf_save_id+"_fmin").val();
            var th=jQuery("#"+ab_po_cf_save_id+"_thour").val();
            var tm=jQuery("#"+ab_po_cf_save_id+"_tmin").val();
            
            // get list of ring selected
            jQuery("input[name='subject[]']").each(function(){
                if (jQuery(this).attr("checked"))
                    ring+=jQuery(this).val()+"_";
            });
            
            
            if (ring == "")
            {
                alert("Bạn chưa chọn vòng cầu nguyện");
                return;
            }
            ajax_loader_show(jQuery("#lbfs"+ab_po_cf_save_id));
            jQuery.post('index.php',{
                module: 'prayonline_createuserformmodule',
                action: 'temp_register',
                ajax_action: 1,
                ring: ring, 
                fh: fh, 
                fm: fm,
                th: th, 
                tm: tm, 
            }, function(data) {
                data=ajax_string_dim(data);
                if (data!="") // load failed
                {
                    // Mar 9, 2011 - trungdt - server now not respone HTML, we must refresh page
                    //jQuery("#f"+ab_po_cf_save_id).fadeToggle("normal");
                    //jQuery(".po_cf_empty").hide();
                    //jQuery("#f"+ab_po_cf_save_id).before(data);
                    //ab_po_cf_active_removebutton();
                    window.location.reload();
                }
                else
                {
                    alert("Xin lỗi, yêu cầu của bạn trùng thời gian với những yêu cầu đăng ký trước của bạn. Vui lòng chọn thời điểm cầu nguyện khác");
                }
                ajax_loader_restore(jQuery("#lbfs"+ab_po_cf_save_id));
            });
        });
        // Feb 26, 2011 - trungdt - we dont need to check hour and min
        jQuery("#"+ab_po_cf_save_id+"_fhour").change(function(){
            ab_po_cf_rebuildInterval();
            ab_po_cf_calculate_finishtime();
        });
        jQuery("#"+ab_po_cf_save_id+"_fmin").change(function(){
            ab_po_cf_rebuildInterval();
            ab_po_cf_calculate_finishtime();
        });
        jQuery("#"+ab_po_cf_save_id+"_interval").change(function(){
            ab_po_cf_calculate_finishtime();
        });
    });
}